<template>
  <el-dialog
    :visible.sync="importModal"
    :title="title"
    class="goods-upload-modal"
  >
    <div
      v-if="uploadMsg.length > 0"
      :closable="false"
      class="erorr-box"
      type="error"
    >
      <div style="color: #f56c6c">
        <div style="position: absolute; right: 10px; top: 10px">
          <i class="el-icon-close" @click="uploadMsg = []" />
        </div>
        <div
          v-for="(item, index) in uploadMsg"
          :key="index"
          style="padding-right: 40px"
          v-html="item"
        />
      </div>
    </div>
    <div class="upload-style">
      <el-upload
        v-loading="loading"
        :data="params"
        :on-error="handleUploadError"
        :file-list="fileList"
        :auto-upload="true"
        :action="baseUrl + actionUrl"
        :on-progress="handleUploadProress"
        :headers="headers"
        :on-success="handleUploadSuccess"
        drag
        class="upload-demo"
        accept=".xlsx,.xls"
        multiple
        style="text-align: center"
      >
        <i class="el-icon-upload"/>
        <div class="el-upload__text">
          {{ '将文件拖到此处，或' }}
          <em>{{ '点击上传' }}</em>
        </div>
      </el-upload>
    </div>
    <span
      slot="footer"
      class="dialog-footer"
      style="display: flex; justify-content: space-around"
    >
      <el-link v-if="tempalteUrl" :href="tempalteUrl" type="primary">
        {{ '下载模板' }}
      </el-link>
      <el-button type="text" @click="importModal = false">{{
        '取消'
      }}</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { getToken } from '@/utils/auth'

export default {
  props: {
    title: {
      type: String,
      default: ''
    },
    actionUrl: {
      type: String,
      default: ''
    },
    tempalteUrl: {
      type: String,
      default: ''
    },
    params: {
      type: Object,
      default: () => {
        return {}
      }
    },
    // 导入成功后跳转的路由
    routerPath: {
      type: String,
      default: '/wms/receiptOrder'
    }
  },
  data() {
    return {
      headers: {
        Authorization: getToken()
      },
      fileList: [],
      uploadMsg: [],
      importModal: false,
      baseUrl: process.env.HOST,
      loading: false
    }
  },
  methods: {
    open() {
      this.importModal = true
      this.fileList = []
    },
    cancel() {
      this.importModal = false
    },
    handleUploadProress(e, file, fileList) {
      this.loading = true
    },
    handleUploadSuccess(res, file) {
      console.log('res', res)
      this.loading = false
      if (res === 'ok') {
        this.uploadMsg = (res.data || []).slice(0, 10)
        if (this.uploadMsg.length === 0) {
          this.$message({
            type: 'success',
            message: '导入成功'
          })
          this.importModal = false
        }
      } else {
        this.$message({
          type: 'error',
          message: '操作失败'
        })
        this.fileList = []
      }
    },
    handleUploadError() {
      this.loading = false
      this.fileList = []
      this.$message({
        type: 'error',
        message: '操作失败'
      })
    }
  }
}
</script>
